<?php
namespace Api\Controller;
use Api\Model\TagModel;
use Api\Model\FileModel;
use Api\Model\ProjectModel;

/**
 * 产品分类控制器，获取产品分类列表及其他信息
 * 
 * @author xiaoyutab<xiaoyutab@qq.com>
 * @version v1.0.0
 * @copyright (c) 2017, xiaoyutab
 * @adtime 2018-06-20 23:10:18
 */
class TypeController extends ApiController{
    /**
     * 获取产品分类接口
     * 
     * @api {post} /Type 01获取产品分类
     * @apiDescription 获取产品分类
     * @apiGroup 02Type
     * @apiVersion 1.0.0
     * @apiSuccessExample Success-Response:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:33:11",
     *      "data":{
     *          "count":"2",// 分类条数
     *          "list":[
     *              {
     *                  "id":"2",// 分类ID
     *                  "name":"再加一个类型"// 分类名称
     *              },
     *              {
     *                  "id":"1",
     *                  "name":"测试类型"
     *              }
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-Empty:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:33:11",
     *      "data":{
     *          "count":"0",// 分类条数
     *          "list":[
     *          ]
     *      }
     *  }
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 200 OK
     *      {
     *          "code":2,
     *          "msg":"参数错误",
     *          "time":"2018-06-20 23:33:11",
     *          "data":{
     *          }
     *      }
     */
    public function indexAction(){
        $tag = new TagModel();
        $list = $tag->where(['type'=> get_config_id('TagType','分类')])->getList(0,999);
        $data = [];
        foreach($list as $v){
            $data[] = [
                'id' => $v['id'],
                'name' => $v['name']
            ];
        }
        return $this->returnCode([
            'count' => count($data),
            'list' => $data,
        ]);
    }
    
    /**
     * 获取产品品牌接口
     * 
     * @api {post} /Type/brand 02获取产品品牌接口
     * @apiDescription 获取产品品牌接口
     * @apiGroup 02Type
     * @apiVersion 1.0.0
     * @apiSuccessExample Success-Response:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:43:56",
     *      "data":{
     *          "count":"1",// 查询到的条数
     *          "list":[
     *              {
     *                  "id":"11",// 品牌编号
     *                  "name":"测试品牌",// 品牌名称
     *                  "file":[// 品牌图片列表
     *                      "image/2018-06-10/2018-06-10/5b1c8c390212f.jpg",
     *                      "image/2018-06-10/2018-06-10/5b1c8c390051c.jpg"
     *                  ],
     *                  "desc":"<p>发所发生的爱是粉色</p> "// 品牌简介,此处为富文本信息
     *              }
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-No-Image:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:43:56",
     *      "data":{
     *          "count":"1",// 查询到的条数
     *          "list":[
     *              {
     *                  "id":"11",// 品牌编号
     *                  "name":"测试品牌",// 品牌名称
     *                  "file":[// 品牌图片列表
     *                  ],
     *                  "desc":"<p>发所发生的爱是粉色</p> "// 品牌简介,此处为富文本信息
     *              }
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-Empty:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:33:11",
     *      "data":{
     *          "count":"0",// 查询到的条数
     *          "list":[
     *          ]
     *      }
     *  }
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 200 OK
     *      {
     *          "code":2,
     *          "msg":"参数错误",
     *          "time":"2018-06-20 23:33:11",
     *          "data":{
     *          }
     *      }
     */
    public function brandAction(){
        $tag = new TagModel();
        $file = new FileModel();
        $list = $tag->where(['type'=> get_config_id('TagType','品牌')])->getList(0,999);
        $data = [];
        foreach($list as $v){
            $data[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'file' => $file->get_img_type('tag', $v['id'], FALSE, 'file_path'),
                'desc' => $v['desc']
            ];
        }
        return $this->returnCode([
            'count' => count($data),
            'list' => $data,
        ]);
    }
    
    
    /**
     * 获取产品案例接口
     * 
     * @api {post} /Type/case 03获取产品案例接口
     * @apiDescription 获取产品案例接口
     * @apiGroup 02Type
     * @apiVersion 1.0.0
     * @apiSuccessExample Success-Response:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:43:56",
     *      "data":{
     *          "count":"1",// 查询到的条数
     *          "list":[
     *              {
     *                  "id":"11",// 案例编号
     *                  "name":"测试案例",// 案例名称
     *                  "file":[// 案例图片列表
     *                      "image/2018-06-10/2018-06-10/5b1c8c390212f.jpg",
     *                      "image/2018-06-10/2018-06-10/5b1c8c390051c.jpg"
     *                  ],
     *                  "desc":"<p>发所发生的爱是粉色</p> "// 案例简介,此处为富文本信息
     *              }
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-No-Image:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:43:56",
     *      "data":{
     *          "count":"1",// 查询到的条数
     *          "list":[
     *              {
     *                  "id":"11",// 案例编号
     *                  "name":"测试品牌",// 案例名称
     *                  "file":[// 案例图片列表
     *                  ],
     *                  "desc":"<p>发所发生的爱是粉色</p> "// 案例简介,此处为富文本信息
     *              }
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-Empty:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:33:11",
     *      "data":{
     *          "count":"0",// 查询到的条数
     *          "list":[
     *          ]
     *      }
     *  }
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 200 OK
     *      {
     *          "code":2,
     *          "msg":"参数错误",
     *          "time":"2018-06-20 23:33:11",
     *          "data":{
     *          }
     *      }
     */
    public function caseAction(){
        $tag = new TagModel();
        $file = new FileModel();
        $list = $tag->where(['type'=> get_config_id('TagType','案例')])->getList(0,999);
        $data = [];
        foreach($list as $v){
            $data[] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'file' => $file->get_img_type('tag', $v['id'], FALSE, 'file_path'),
                'desc' => $v['desc']
            ];
        }
        return $this->returnCode([
            'count' => count($data),
            'list' => $data,
        ]);
    }
    
    /**
     * 获取产品列表接口
     * 
     * @api {post} /Type/get_list 04获取产品列表接口
     * @apiDescription 获取产品列表接口
     * @apiGroup 02Type
     * @apiParam {integer} id 案例/分类/品牌的返回id编号
     * @apiVersion 1.0.1
     * @apiSuccessExample Success-Response:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:43:56",
     *      "data":{
     *          "count":"1",// 查询到的条数
     *          "list":[
     *              {
     *                  "id": "5",// 产品编号
     *                  "name": "测试名称",// 产品名称
     *                  "price": "111.00",// 产品价格
     *                  "wl_no": "wl0100252",// 物料编号
     *                  "content": "&lt;p&gt;撒的发生的GV是否GV的撒GV&lt;/p&gt;\r\n",// 产品简介
     *                  "file": [// 产品图片列表
     *                      "image/2018-06-11/2018-06-11/5b1dde927e4f2.jpg",
     *                      "image/2018-06-11/2018-06-11/5b1dde927c383.jpg",
     *                      "image/2018-06-11/2018-06-11/5b1dde9278bce.jpg"
     *                  ],
     *                  "parent_id": "5",// 产品所属分类编号，该编号为（分类/品牌/案例/首页）的编号
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-No-Image:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:43:56",
     *      "data":{
     *          "count":"1",// 查询到的条数
     *          "list":[
     *              {
     *                  "id": "5",// 产品编号
     *                  "name": "测试名称",// 产品名称
     *                  "price": "111.00",// 产品价格
     *                  "wl_no": "wl0100252",// 物料编号
     *                  "content": "&lt;p&gt;撒的发生的GV是否GV的撒GV&lt;/p&gt;\r\n",// 产品简介
     *                  "file": [// 产品图片列表
     *                  ],
     *                  "parent_id": "5",// 产品所属分类编号，该编号为（分类/品牌/案例/首页）的编号
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-Empty:
     *  HTTP/1.1 200 OK
     * {
     *      "code":0,
     *      "msg":"成功",
     *      "time":"2018-06-20 23:33:11",
     *      "data":{
     *          "count":"0",// 查询到的条数
     *          "list":[
     *          ]
     *      }
     *  }
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 200 OK
     *      {
     *          "code":2,
     *          "msg":"参数错误",
     *          "time":"2018-06-20 23:33:11",
     *          "data":{
     *          }
     *      }
     */
    public function get_listAction(){
        $tag = new TagModel();
        $project = new ProjectModel();
        $id = I('post.id',0,'intval');
        $info = $tag->get_info($id);
        if($id['status'] == 98){
            // 如果信息已删除则直接返回参数错误
            return $this->returnCode('2');
        }
        // 获取关联产品列表
        $project_list = $tag->get_link()->where([
            'file_id' => $id,
            'target_name' => $tag->get_table_id('project'),
        ])->getList(0,999);
        $return_data = [];
        foreach($project_list as $k => $v){
            $temp = $project->get_info($v['target_id']);// 获取案例详情
            // 整理案例数据
            $return_data[$v['target_id']] = [
                'id' => $temp['id'],
                'name' => $temp['name'],
                'price' => $temp['price'],
                'wl_no' => $temp['wl_no'],
                'content' => $temp['content'],
                'file' => $temp['file'],
                'parent_id' => $id,
            ];
        }
        return $this->returnCode([
            'count' => count($return_data),
            'list' => array_values($return_data)
        ]);
    }
    
    /**
     * 获取产品列表接口
     * 
     * @api {post} /Type/get_search 05搜索产品信息
     * @apiDescription 搜索产品信息
     * @apiGroup 02Type
     * @apiParam {string} keywords 搜索的关键词信息
     * @apiVersion 1.0.0
     * @apiSuccessExample Success-Response:
     *  HTTP/1.1 200 OK
     *  {
     *      "code": 0,
     *      "msg": "成功",
     *      "time": "2018-06-26 15:21:34",
     *      "data": {
     *          "pinpai_list": [// 品牌列表
     *              {
     *                  "name": "测试品牌",// 品牌名称
     *                  "desc": "",// 品牌简介
     *                  "file": [// 品牌图片列表
     *                      "image/2018-06-19/2018-06-19/5b28bda4ca3c8.jpg"
     *                  ],
     *                  "id": "9"// 品牌编号
     *              }
     *          ],
     *          "chanpin_list": [
     *              {
     *                  "id": "5",// 产品编号
     *                  "name": "测试分类111",// 产品名称
     *                  "price": "123.00",// 产品价格
     *                  "wl_no": "水电费",// 产品物料编号
     *                  "content": "&lt;p&gt;萨法VBV型&lt;/p&gt;\r\n",// 产品描述信息
     *                  "file": [// 产品图片列表
     *                      "image/2018-06-19/2018-06-19/5b28bda4ca3c8.jpg",
     *                      "image/2018-06-11/2018-06-11/5b1dde9278bce.jpg"
     *                  ]
     *              }
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-NoFile:
     *  HTTP/1.1 200 OK
     *  {
     *      "code": 0,
     *      "msg": "成功",
     *      "time": "2018-06-26 15:21:34",
     *      "data": {
     *          "pinpai_list": [// 品牌列表
     *              {
     *                  "name": "测试品牌",// 品牌名称
     *                  "desc": "",// 品牌简介
     *                  "file": [// 品牌图片列表
     *                  ],
     *                  "id": "9"// 品牌编号
     *              }
     *          ],
     *          "chanpin_list": [
     *              {
     *                  "id": "5",// 产品编号
     *                  "name": "测试分类111",// 产品名称
     *                  "price": "123.00",// 产品价格
     *                  "wl_no": "水电费",// 产品物料编号
     *                  "content": "&lt;p&gt;萨法VBV型&lt;/p&gt;\r\n",// 产品描述信息
     *                  "file": [// 产品图片列表
     *                  ]
     *              }
     *          ]
     *      }
     *  }
     * @apiSuccessExample Success-Empty:
     *  HTTP/1.1 200 OK
     *  {
     *      "code": 0,
     *      "msg": "成功",
     *      "time": "2018-06-26 15:21:34",
     *      "data": {
     *          "pinpai_list": [// 品牌列表
     *          ],
     *          "chanpin_list": [
     *          ]
     *      }
     *  }
     * @apiErrorExample Error-Empty-Parameter:
     * HTTP/1.1 200 OK
     * {
     *     "code":24,
     *     "msg":"参数不能为空",
     *     "time":"2018-06-20 23:33:11",
     *     "data":{
     *     }
     * }
     */
    public function get_searchAction(){
        $key = I('post.keywords','','trim');
        if(empty($key)){
            // 如果参数为空就报错误异常
            return $this->returnCode(24);
        }
        $_tag = new TagModel();
        $_project = new ProjectModel();
        $_file = new FileModel();
        $return_data = [
            'pinpai_list' => [],
            'chanpin_list' => []
        ];
        $where = [];
        foreach (explode(' ', $key) as $v){
            $where[] = '%'.$v.'%';
        }
        $where = ['name'=>['like',$where]];
        $tag_list = $_tag->where($where)->where(['type'=> get_config_id('TagType','品牌')])->getList(0,999);
        foreach($tag_list as $v){
            $return_data['pinpai_list'][] = [
                'name' => $v['name'],
                'desc' => $v['desc'],
                'file' => $_file->get_img_type('tag', $v['id'],FALSE,'file_path'),
                'id' => $v['id'],
            ];
        }
        $project_list = $_project->where($where)->getList(0,999);
        foreach($project_list as $v){
            $return_data['chanpin_list'][] = [
                'id' => $v['id'],
                'name' => $v['name'],
                'price' => $v['price'],
                'wl_no' => $v['wl_no'],
                'content' => $v['content'],
                'file' => $_file->get_img_type('project', $v['id'], FALSE, 'file_path'),
            ];
        }
        return $this->returnCode($return_data);
    }
}
